<?php

namespace App\Model\Api;

use App\Model\BaseModel;
use App\Model\Number;
use Illuminate\Support\Facades\DB;

class FamilyModel extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'family';
    protected $primaryKey = 'family_member_id';
    
    public function createData($data) {
        return curl_request(getKkaudioApiUrl().'/inner/family/upsert', $data, 'json', 'POST');
    }
    
    public function deleteData($id) {
        return curl_request(getKkaudioApiUrl().'/inner/family/remove',['family_id' => $id], 'urlencoded', 'GET');
    }
    
    public function familyMember() {
        return $this->hasMany(FamilyMemberModel::class,'family_id','family_id');
    }
    
    public function number() {
        return $this->belongsTo(Number::class,'user_id','user_id');
    }


    public function getFamilyRoomList($f_id)
    {
        $res = DB::connection($this->connection)->table('family_room')->where(['family_id' => $f_id])
            ->leftJoin("room", "room.room_id", "=", "family_room.room_id")
            ->select('family_room.*','room_uid','notice','image','desc','name')
            ->get();
        return $this->objToArray($res);
    }
}
